﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel;
using Microsoft.Phone.Shell;
using Microsoft.Phone.Controls;
using System.Windows.Threading;

namespace Code4WP7SDK
{
    public class GlobalLoading : INotifyPropertyChanged
    {
        #region Fields
        //全局的progress
        private ProgressIndicator _mangoIndicator;
        //当前页面
        private PhoneApplicationPage _currentPage;
        //词条数组
        private string[] AdvWords;
        //定时显示词条的计时器
        private DispatcherTimer _timer;
        //当前的词条索引
        private int _currentIndex;
        //是否是开始的词条
        private bool _isStartAdv;
        //是否显示忙碌
        private bool _isBusy;
        private string _text;
        #endregion

        #region Constructor
        private GlobalLoading()
        {
            AdvWords = new string[] {"SystemTray是个好东东哦,亲","她可以用来打广告的哦","她也可以用来显示进度",
                "至于你信不信","I do believe!!!" };
        }
        //Singleton
        private static GlobalLoading _instance;
        public static GlobalLoading Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new GlobalLoading();
                }
                return _instance;
            }
        }
        #endregion

        #region Properties
        public int TotalCount
        {
            get
            {
                return AdvWords.Length;
            }
        }
        public int CurrentIndex
        {
            get { return _currentIndex; }
            set
            {
                _currentIndex = value;
                _mangoIndicator.Text = AdvWords[value];
                _mangoIndicator.IsVisible = true;
                NotifyPropertyChanged("CurrentIndex");
            }
        }
        public bool IsStartAdv
        {
            get { return _isStartAdv; }
            set
            {
                _isStartAdv = value;
                _mangoIndicator.IsVisible = value;
                if (value)
                {
                    StartTimer();
                }
                else
                {
                    StopTimer();
                }
            }
        }
        public bool IsBusy
        {
            get { return _isBusy; }
            set
            {
                _isBusy = value;
                Deployment.Current.Dispatcher.BeginInvoke(() =>
                    {
                        if (_currentPage != null)
                        {
                            _currentPage.SetValue(SystemTray.IsVisibleProperty, value);
                            _mangoIndicator.IsVisible = value;
                            _mangoIndicator.IsIndeterminate = value;
                        }
                    });
            }
        }
        public string Text
        {
            get { return _text; }
            set
            {
                _text = value;
                Deployment.Current.Dispatcher.BeginInvoke(() =>
                {
                    if (_currentPage != null)
                    {
                        _mangoIndicator.IsVisible = true;
                        _mangoIndicator.Text = value;
                    }
                });
            }
        }
        #endregion

        #region Method
        public void InitialLize(PhoneApplicationFrame frame)
        {
            _mangoIndicator = new ProgressIndicator();
            //这里也可以注册navigating事件
            frame.Navigated += frame_Navigated;
        }

        void frame_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e)
        {
            _currentPage = e.Content as PhoneApplicationPage;
            if (_currentPage != null)
            {
                _currentPage.SetValue(SystemTray.IsVisibleProperty, false);
                _currentPage.SetValue(SystemTray.ProgressIndicatorProperty, _mangoIndicator);
                _currentPage.SetValue(SystemTray.OpacityProperty,0.9);
                //TODO 颜色的设置可以作为属性传入
                _currentPage.SetValue(SystemTray.BackgroundColorProperty, AppColorHelper.GetPhoneForeGround().Color); //TODO 根据系统去设置颜色变化
                //_currentPage.SetValue(SystemTray.ForegroundColorProperty, AppColorHelper.GetAppAccentColor().Color);
            }
        }
        private void StopTimer()
        {
            if (_timer != null)
            {
                _timer.Stop();
                _timer = null;
            }
        }

        private void StartTimer()
        {
            if (null == _timer)
            {
                _timer = new DispatcherTimer();
                _timer.Interval = TimeSpan.FromSeconds(2);//2秒钟更新一次
            }
            _timer.Tick += (s, e) =>
            {
                if (_currentIndex == TotalCount - 1)
                {
                    _currentIndex = 0;
                }
                CurrentIndex++;
            };
            _timer.Start();
        }
        #endregion

        #region NotifyPropertyChanged
        public event PropertyChangedEventHandler PropertyChanged;
        public void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion
        
    }


}
